<template>
<div class="ivu-card ivu-card-bordered ivu-card-dis-hover">
  <div class="ivu-card-head"><p>{{ $route.meta.title }}</p></div>

  <div class="ivu-card-body">
    <iForm ref="formRef" :model="formFields" :rules="rules" :label-width="200">

      <FormItem prop="name" label="名称(必须英文)：">
        <Input v-model="formFields.name" placeholder="名称(必须英文)"/>
      </FormItem>

      <FormItem prop="description" label="描述：">
        <Input v-model="formFields.description" placeholder="描述"/>
      </FormItem>

      <FormItem>
        <Button type="primary" :loading="submitLoading" @click="onSubmit">{{ $route.meta.title }}</Button>
        <Button type="ghost" @click="$router.go(-1);">返回</Button>
      </FormItem>
    </iForm>
  </div>
</div>
</template>
<script>
import qs from 'qs';
import { Form, Input, Button, Select, Option, Message } from 'iview';
import ajaxiView from '@/utils/ajaxiView';
import auth from '@/utils/auth';

export default {
  components: {
    [Form.name]: Form,
    [Form.Item.name]: Form.Item,
    [Input.name]: Input,
    [Button.name]: Button,
    [Select.name]: Select,
    [Option.name]: Option
  },
  data() {
    return {
      token: auth.getToken(),
      submitLoading: false,
      formFields: {
        status: '0'
      },
      rules: {
        name: [
          {type:'string', required: true, message: '请输入名称!', trigger: 'blur'}
        ],
        description: [
          {type:'string', required: true, message: '请输入描述!', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    onSubmit() {
      var _this = this;
      _this.$refs.formRef.validate((valid) => {
        _this.submitLoading = true;
        if(valid) {
          ajaxiView.post('/backstage/account/roles?token='+this.token, qs.stringify(_this.formFields)).then(function(data) {
            if(data !== false) {
              Message.success({
                content: data.msg,
                onClose: function() {
                  _this.$router.push({ name: 'backendAccountRolesIndex' });
                }
              });
            }

            _this.submitLoading = false;
          });
        } else {
          _this.submitLoading = false;
        }
      });
    }
  }
}
</script>
